package hadoop.hdfs.demo;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;

import java.util.HashMap;

/**
 * @author qiss
 * @create 2018-02-12 17:34
 * job.setPartitionerClass(ProvincePartitioner.class);
 **/
public class ProvincePartitioner extends Partitioner<Text,BeanComparable> {

    static HashMap<String, Integer> provinceMap = new HashMap<String, Integer>();
    static {
        provinceMap.put("135", 0);
        provinceMap.put("136", 1);
        provinceMap.put("137", 2);
        provinceMap.put("138", 3);
        provinceMap.put("139", 4);

    }

    @Override
    public int getPartition(Text key, BeanComparable beanComparable, int numPartitions) {
        Integer code = provinceMap.get(key.toString().substring(0, 3));
        return code == null ?5:code;
    }
}
